﻿/**
 * 本地数据缓存
 */
app.loading.set(0, () => cache.load());

/**
 * 本地数据缓存
 */
var cache = new (class _Cache_ {
    constructor() {
        this.parameters = [];
    }

    set(data) {
        this.parameters = data;
        this.root = this.parameters.get({ key: 'root' }, null, true);
        this.top = this.parameters.get(x => x.parent && x.parent.key === 'root');
        this.dataState = this.parameters.get(x => x.parent && x.parent.key === 'dataState');
        this.authorizeType = this.parameters.get(x => x.parent && x.parent.key === 'authorizeType');
        this.assignType = this.parameters.get(x => x.parent && x.parent.key === 'assignType');
        this.identityType = this.parameters.get(x => x.parent && x.parent.key === 'identityType');
        this.resourceType = this.parameters.get(x => x.parent && x.parent.key === 'resourceType');
        this.allowType = this.parameters.get(x => x.parent && x.parent.key === 'allowType');
        this.accessLevel = this.parameters.get(x => x.parent && x.parent.key === 'accessLevel');
        this.authType = this.parameters.get(x => x.parent && x.parent.key === 'authType');
        this.yesOrNo = this.parameters.get(x => x.parent && x.parent.key === 'yesOrNo');
        this.haveOrNot = this.parameters.get(x => x.parent && x.parent.key === 'haveOrNot');
        this.agreeOrNot = this.parameters.get(x => x.parent && x.parent.key === 'agreeOrNot');
    }

    load() {
        let parameters = localCache.global.get('parameters');
        if ($.com.isArray(parameters)) {
            this.set(parameters);
        } else {
            return axios.get('/odata/parameters?$select=id,index,key,value,text,parentid&$expand=parent($select=id,key)&$orderby=index')
                .then(resp => resp.data)
                .then(data => {
                    data = $.com.dataAdapter(data);
                    if ($.com.isArray(data)) {
                        localCache.global.set('parameters', data)
                        this.set(data);
                    }
                });
        }
    }
})();